#pragma once
#include <iostream>
#include <unordered_map>
#include <functional>
#include <sys/types.h>
#include <unistd.h>

using namespace std;
using task_t = function<void()>;

void DownLoad()
{
    cout << "-------------------------------------" << endl;
    cout << getpid() <<" Working DownLoad Task" << endl;
}

void Log()
{
    cout << "-------------------------------------" << endl;
    cout  << getpid() << " Working Log Task" << endl;
}

static int number = 0;
class TaskManger
{
public:
    TaskManger()
    {
        InsertTask(DownLoad);
        InsertTask(Log);
    }

    void InsertTask(task_t t)
    {
        tasks[number++] = t;
    }

    int SelectTask()
    {
        return rand() % number;
    }

    void Excute(int number)
    {
        if (tasks.find(number) == tasks.end())
            return;
        tasks[number]();
    }

    ~TaskManger()
    {
    }

private:
    unordered_map<int, task_t> tasks;
};
TaskManger tm;
void Worker()
{
    while (true)
    {
        int cmd = 0;
        int n = ::read(cmd, &cmd, sizeof(cmd));
        if (n == sizeof(cmd))
        {
            tm.Excute(cmd);
        }
        else if (n == 0)
        {
            cout << getpid() << "->pid  read nothing "  << endl;
            break;
        }
        else
        {
        }
    }
}